package com.erp.erp_servers.Blueprint.imp;

import com.erp.erp_dao.Blueprint.BlueprintContentFileMapper;
import com.erp.erp_entitys.Blueprint.BlueprintContentFile;
import com.erp.erp_entitys.Blueprint.req.AddContentFileReq;
import com.erp.erp_servers.Blueprint.IBlueprintContentFileService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zjk
 * @version 1.0
 * @description
 * @date 2021-09-26 0:00
 */
@Service
public class BlueprintContentFileServiceImpl implements IBlueprintContentFileService {

    private final BlueprintContentFileMapper contentFileDao;

    public BlueprintContentFileServiceImpl(BlueprintContentFileMapper contentFileDao) {
        this.contentFileDao = contentFileDao;
    }


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return contentFileDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(BlueprintContentFile record) {
        return contentFileDao.insert(record);
    }

    @Override
    public Boolean insertSelective(AddContentFileReq req) throws Exception {
        try {
            List<BlueprintContentFile> newFiles = req.getFiles();
            List<BlueprintContentFile> oldFiles = contentFileDao.findContentFileByParam(req.getBlueprintId(), req.getDetailId());

            List<Integer> newIds=newFiles.stream().map(p -> p.getId()).collect(Collectors.toList());
            List<Integer> oldIds=oldFiles.stream().map(p -> p.getId()).collect(Collectors.toList());
            if (newIds.size()!=0){
                //获得删除的元素
                oldIds.removeAll(newIds);
            }
            //直接删除明细
            if (oldIds.size()!=0){
                contentFileDao.deleteByIds(oldIds);
            }
            for (BlueprintContentFile p:newFiles){
                p.setBlueprintId(req.getBlueprintId());
                p.setDetailId(req.getDetailId());

                //判断ID为空则添加，负责修改
                if (p.getId() == null){
                    contentFileDao.insertSelective(p);
                }else{
                    contentFileDao.updateByPrimaryKeySelective(p);
                }

            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("保存图纸附件异常！！");
        }
    }

    @Override
    public BlueprintContentFile selectByPrimaryKey(Integer id) {
        return contentFileDao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(BlueprintContentFile record) {
        return contentFileDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(BlueprintContentFile record) {
        return contentFileDao.updateByPrimaryKey(record);
    }

    @Override
        public List<BlueprintContentFile> findContentFileByParam(Integer blueprintId, Integer detailId) {
        List<BlueprintContentFile> files = contentFileDao.findContentFileByParam(blueprintId, detailId);
        return files;
    }
}
